package com.example.warehouse.bus.controller;

import com.example.warehouse.bus.pojo.param.BusRepertoryAddGoodsNum;
import com.example.warehouse.bus.pojo.param.BusRepertoryReduceGoodsNumParam;
import com.example.warehouse.bus.service.IBusRepertoryService;
import com.example.warehouse.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 处理库存的控制器
 * 主要功能有增减库存数量，分页查询库存数据，删除库存某条数据（这里不涉及修改库存数据）
 */
@Slf4j
@RestController
@RequestMapping("repertory")
@Api(tags = "4.1 库存管理模块")
public class BusRepertoryController {
    @Autowired
    private IBusRepertoryService ibrs;

    @ApiOperation("增加库存")
    @ApiOperationSupport(order = 10)
    @PostMapping("add")
    public JsonResult addGoodsNumber(BusRepertoryAddGoodsNum busRepertoryAddGoodsNum) {
        log.debug("BusRepertoryController控制器开始处理新增库存的请求,请求参数是:{}", busRepertoryAddGoodsNum);
        ibrs.addGoodsNum(busRepertoryAddGoodsNum);
        return JsonResult.ok();
    }

    @ApiOperation("减少库存")
    @ApiOperationSupport(order = 20)
    @PostMapping("reduce")
    public JsonResult reduceGoodsNumber(BusRepertoryReduceGoodsNumParam busRepertoryReduceGoodsNumParam) {
        log.debug("BusRepertoryController控制器开始处理减少库存的请求,请求参数是:{}", busRepertoryReduceGoodsNumParam);
        ibrs.reduceGoodsNum(busRepertoryReduceGoodsNumParam);
        return JsonResult.ok();
    }

    @ApiOperation("删除库存")
    @ApiOperationSupport(order = 30)
    @PostMapping("delete")
    public JsonResult deleteById(Long id) {
        log.debug("BusRepertoryController控制器开始处理根据id删除该条数据的请求,请求参数是:{}", id);
        ibrs.deleteByIdGoods(id);
        return JsonResult.ok();
    }

    @ApiOperation("库存列表")
    @ApiOperationSupport(order = 40)
    @GetMapping("list")
    public JsonResult selectStockList(Integer pageNum) {
        log.debug("开始处理查询库存列表的请求！");
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        return JsonResult.ok(ibrs.pageHelperByPageNum(pageNum));
    }

    @ApiOperation("关键字搜索")
    @ApiOperationSupport(order = 50)
    @GetMapping("fuzzy-query")
    public JsonResult fuzzyQueryBuGoodsName(Integer pageNum, String keyword) {
        log.debug("开始处理模糊查询库存列表的请求！");
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        return JsonResult.ok(ibrs.pageHelperByPageNumAndGoodsNameFuzzyQuery(pageNum, keyword));
    }
}
